package com.neuedu.controller;

import com.neuedu.bean.Cart;
import com.neuedu.bean.User;
import com.neuedu.common.R;
import com.neuedu.mapper.UserMapper;
import com.neuedu.service.CartService;
import com.neuedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 1. 将该类转换控制器类，并设置该中的方法返回值全部都是json数据
//@Controller
//@ResponseBody
@RestController
// 2. 设置该模块的请求路径，如/user
@RequestMapping("/cart")
// 3. 解决跨域
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 获取用户购物车信息
     * @param cart
     * @return
     */
    @PostMapping("/listCart")
    public R listCart(Cart cart){
        try {
            List<Cart> cartList = cartService.listCart(cart);
            return R.ok().data(cartList);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/saveCart")
    public R saveCart(Cart cart){
        try {
            cartService.saveCart(cart);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/updateCart")
    public R updateCart(Cart cart){
        try {
            cartService.updateCart(cart);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/removeCart")
    public R removeCart(Cart cart){
        try {
            cartService.removeCart(cart);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
